package com.xinlu.common.game.dao;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.map.ListOrderedMap;
import org.apache.commons.lang.StringUtils;

import com.xinlu.common.game.bo.ConstantLstBo;
import com.xinlu.common.game.value.BankBackVo;
import com.xinlu.derek.common.BaseDao;
import com.xinlu.persistence.BankBack;
import com.xinlu.util.tool.PageObject;

public class BankBackDao extends BaseDao {

	/**
	 * 
	 * Function :
	 * 
	 * @param username :
	 *            用户名
	 * @param type :
	 *            游戏类型 (CQSSCZXDS) 彩种类型 + 游戏类型
	 * @param addmoney ：
	 *            投注金额
	 * @param oldmoney :
	 *            购买前用户的金额
	 * @param nowmoney :
	 *            用户现在的金额
	 * @param pro_num ：
	 *            单证号
	 * @param cpnum ：
	 *            期号
	 * @param bz ：
	 *            备注
	 * @param states ：
	 *            状态 Derek 2011-5-15 下午01:17:07
	 */
	public void saveTzjlInfoInBankBack(String username, String type, double addmoney, double oldmoney, double nowmoney, String pro_num, String cpnum, int bz, String states) {

		super.saveByProc("{ call dbo.www5007cn(?, ?, ?, ?, ?, ?, ?, ?, ?) }", new Object[] { username, type, addmoney, oldmoney, nowmoney, pro_num, cpnum, bz, states });
	}

	/**
	 * 获取已经取款数
	 * 
	 * @param username
	 * @param bgnDate
	 * @param endDate
	 * @return
	 */
	public int getYjqk(String username, String bgnDate, String endDate) {

		String cmdText = "from BankBack where type='取款' and username='" + username + "' and addtime between '" + bgnDate + "' and '" + endDate + "' ";
		List lst = super.find(cmdText);
		if (lst == null) {
			return 0;
		} else {
			return lst.size();
		}
	}

	/**
	 * 加载存取款列表
	 * 
	 * @param lszLeixing
	 * @param bgnDate
	 * @param endDate
	 * @return
	 */
	public PageObject loadCqklst(String yhm, String lszLeixing, String bgnDate, String endDate, int pageNo) {

		if ("--全部--".equals(lszLeixing) || "存款".equals(lszLeixing) || "取款".equals(lszLeixing)) {

			String cmdText = "select * from Bank_Back where username='" + yhm + "' and addtime>='" + bgnDate + "' and addtime<= '" + endDate + "'";
			if ("--全部--".equals(lszLeixing)) {
				cmdText += " and (type='取款' or type='存款') ";
			} else {
				cmdText += " and type='" + lszLeixing + "' ";
			}
			cmdText+=" order by id desc ";

			return super.findPageObjectByProc(10, pageNo, cmdText, BankBack.class);
		}

		return new PageObject();
	}

	/**
	 * 加载投注记录
	 * 
	 * @param caiZhong
	 * @param lszLeixing
	 * @param bgnDate
	 * @param endDate
	 * @return
	 */
	public PageObject loadTzlst(String yhm, String caiZhong, String lszLeixing, String bgnDate, String endDate, int pageNo) {

		if ("存款".equals(lszLeixing) || "取款".equals(lszLeixing)) {
			return new PageObject();
		}

		String cmdText = "select b.*,w.wfjczwm from Bank_Back as b inner join Wflx as w on w.wfjc=b.type and username='" + yhm + "' and b.addtime >='" + bgnDate + "' and b.addtime <='" + endDate
				+ "' ";
		if (!"--全部--".equals(lszLeixing)) {
			cmdText += " and b.follows like '%" + lszLeixing + "%' ";
		}
		if (!"--全部--".equals(caiZhong)) {
			cmdText += " and b.type like '%" + caiZhong + "%' ";
		}
		cmdText+=" order by b.id desc ";
			
		return super.findPageObjectByProc(10, pageNo, cmdText, BankBackVo.class);
	}

	/**
	 * 加载团队报表的存取款
	 * 
	 * @param yhm
	 * @param cqk
	 * @param bgnDate
	 * @param endDate
	 * @return
	 */
	public Double loadTdbbCqk(String yhm, String cqk, String bgnDate, String endDate) {
		String cmdText = "select ";
		if ("存款".equals(cqk)) {
			cmdText += " sum(backMoney2) ";
		} else {
			cmdText += " sum(backMoney) ";
		}
		cmdText += " from BankBack where addtime between '" + bgnDate + "' and '" + endDate + "' and state=1 and username ='" + yhm + "'  and type ='" + cqk + "'  ";

		List value = super.find(cmdText);
		if (value == null || value.size() != 1) {
			return 0D;
		} else {
			return (Double) value.get(0);
		}
	}

	/**
	 * 加载团队报表投注记录
	 * 
	 * @param yhm
	 * @param caiZhong
	 * @param lszLeiXing
	 * @param bgnDate
	 * @param endDate
	 * @return
	 */
	public Double loadTdbbTzjl(String yhm, String caiZhong, String lszLeiXing, String bgnDate, String endDate) {
		String cmdText = "select ";
		// 代购 派奖 返点，除了代购之外都是同级backmoney2
		if ("代购".equals(lszLeiXing)) {
			cmdText += " sum(back_money) ";
		} else {
			cmdText += " sum(back_money2) ";
		}
		cmdText += "from bank_back where addtime>='" + bgnDate + "' and addtime<='" + endDate + "' and prostate='正常' and username ='" + yhm + "' and follows like '%" + lszLeiXing + "%'";

		if (!"--全部--".equals(caiZhong)) {
			cmdText += "and type like '" + caiZhong + "%'";
		}

		List value = super.query(cmdText);
		ListOrderedMap lom = (ListOrderedMap) value.get(0);
		Object v = lom.getValue(0);
		if (ConstantLstBo.isNullOrEmpty(v)) {
			return 0D;
		} else {
			return ((BigDecimal) v).doubleValue();
		}
	}

	/**
	 * 获取团队流水账
	 * 
	 * @param yhms
	 * @param dzh
	 * @param qh
	 * @param lszLeiXing
	 * @param bgnDate
	 * @param endDate
	 * @return
	 */
	public PageObject loadTdLsz(List yhms, String dzh, String qh, String lszLeiXing, String bgnDate, String endDate, int pageNo) {

		String cmdText = "select lotteryid,addtime,username,expect,back_Money as backmoney,back_Money2 as backmoney2,b_after as bafter,prostate from Bank_Back where addtime>='" + bgnDate
				+ "' and addtime<='" + endDate + "' ";
		if ("--全部--".equals(lszLeiXing)) {
			// 如果为全部，则通过.
		} else if ("存款".equals(lszLeiXing) || "取款".equals(lszLeiXing)) {
			cmdText += " and type='" + lszLeiXing + "' ";
		} else {
			cmdText += " and follows like '%" + lszLeiXing + "%' ";
		}

		if (StringUtils.isNotEmpty(dzh)) {
			cmdText += " and lotteryid='" + dzh + "' ";
		}

		if (StringUtils.isNotEmpty(qh)) {
			cmdText += " and expect='" + qh + "' ";
		}

		List<String> yhmsCondition = new ArrayList<String>();
		for (int i = 0; i < yhms.size(); i++) {
			yhmsCondition.add(" username='" + yhms.get(i) + "' ");
		}

		cmdText += " and ( " + StringUtils.join(yhmsCondition.toArray(), "or") + " )";
		cmdText+=" order by id desc ";

		return super.findPageObjectByProc(10, pageNo, cmdText, BankBackVo.class);
	}

	/**
	 * 加载流水账
	 * 
	 * @param dzh
	 * @param qh
	 * @return
	 */
	public BankBack loadLsz(String dzh) {
		String cmdText = "from BankBack where lotteryid='" + dzh + "' ";

		List lst = super.find(cmdText);
		if (lst != null && lst.size() > 0) {
			return (BankBack) lst.get(0);
		} else {
			return null;
		}
	}

	/**
	 * 更新流水账
	 * 
	 * @param instance
	 */
	public void update(BankBack instance) {

		super.update(instance);
	}

	/**
	 * 保存流水账
	 * 
	 * @param instance
	 */
	public void save(BankBack instance) {

		super.save(instance);
	}

}
